/**
 * siderbarComp
 * Created by mk on 2017-05-04
 */
 //***********************侧边栏组件开始***********************01
Comps.SidebarComp = function(config){
	this.compId = config.compId;
	this.config = config;
}

//初始化数据
Comps.SidebarComp.prototype.init = function(){
	var that = this;
	$.ajax({
		type: "GET",
		"url":ContextPath+"/sidebarComp/ajaxLoadData",
        data: {},
        dataType: "json",
        "success":function(data){
        	var compId = that.config.compId;
        	if(null != data){
        		if('0' == data.type){
        			$("#"+compId+"_screen").attr("data-url", data.skipUrl).attr("data-type", data.type).attr("href","javascript:skip('"+data.skipUrl+"','"+data.type+"');");
        			$("#"+compId+"_screen").html("<div class='m-box'><h3>"+data.title+"</h3><div class='m-qr' ><img src='"+data.iconUrl+"' ></div></div>");
        			
        		}else{
        			$("#"+compId+"_screen").attr("data-url", data.uuid).attr("data-type", data.type).attr("href","javascript:skip('"+data.uuid+"','"+data.type+"');");
        			$("#"+compId+"_screen").html("<div class='m-box'><h3>"+data.title+"</h3><div class='m-qr'><img src='"+data.iconUrl+"'></div></div>");
        		}
        	}else{
        		$("#"+compId+"_screen").hide();
        	}
        }
    });
	
	//同步加载
	that.loadData();
	
	this.bindEvent();
	this.skip("", "");
	
}

Comps.SidebarComp.prototype.loadData = function () {
    var compId = this.config.compId;
    var mobileJomoo = this.config.mobileJomoo;
    var mobileJomoomemo = this.config.mobileJomoomemo;
    var investigation = this.config.investigation;
    var repairhref = this.config.repairhref;
   
    //赋值
    $("#"+compId+"_mobileJomoomemo").html(mobileJomoomemo);
    $("#"+compId+"_repairhref").attr("href",ContextPath + repairhref);
    $("#"+compId+"_investigation").attr("href",ContextPath + investigation);
    
    //渲染二维码
    var qrcode = new QRCode(document.getElementById(compId+"_mobileJomoo"),{
    	  text: mobileJomoo,
    	  width: 117,
    	  height: 117
    }); 
    
}

//绑定JS
Comps.SidebarComp.prototype.bindEvent = function(){
	var that = this;
    //显示悬浮数据
    $(".fixed_right a").hover(function(){
    	$(this).addClass('active');
    },function(){
    	$(this).removeClass('active');
    });
  //返回首页 
	$(".m-return").click(function(){
	   $('body,html').animate({scrollTop:0},500);
	   return false;
	});
	
	$(".m-screen").click(function(){
		var dataUrl = $(this).attr("data-url");
		var dataType = $(this).attr("data-type")
		
		that.skip(dataUrl, dataType);
	});
}

// 随屏签到
Comps.SidebarComp.prototype.skip = function(url, type){
	var that = this;
	if('0' == type){
		var loginUrl = ContextPath+"/front/productList/isLogin"
		$.get(loginUrl,{ranNum:Math.random()}, function(data){
			if("0"==data) {
                var test = window.location.href;
                //如果目前已经是登陆页，则直接到登录页，如果是其他页面则弹出登录框
                if (test.indexOf('/customer/login') >= 0 || test.indexOf('/customer/toLogin') >= 0) {
                    window.location.reload();
                } else {
                    $("#modal-login-form").modal();
                }
			}else{
				// 是签到链接
				if(url.indexOf("/usercenter/customersign/customerSigns")>=0){
					$.post(url, function(data) {
						if (data == '' || data == null || data == undefined) {
							alert("签到失败！");
							location.reload(); // 刷新当前页面
						}
						
						//签到成功
						if("success" == data.success) {
							var msg = "您已连续签到" + data.sustainSignTimes + "天," + "本次签到获得" + data.integral + "积分"
							alert(msg);
							location.reload(); // 刷新当前页面
						}
						
						if("noRules" == data.success) {
							alert("暂无规则，签到失败！");
							location.reload(); // 刷新当前页面
						}
						if("haveSigned" == data.success) {
							alert("已签到！");
							location.reload(); // 刷新当前页面
						}
						flag = true; 
					},"json")
				}else{
					window.location.href = url;
				}
			}
		})
		
	}else if('1'== type){
		//随屏红包获取优惠券券
		var loginUrl = ContextPath+"/front/productList/isLogin"
		$.get(loginUrl,{ranNum:Math.random()}, function(data){
			if("0"==data) {
                var test = window.location.href;
                //如果目前已经是登陆页，则直接到登录页，如果是其他页面则弹出登录框
                if (test.indexOf('/customer/login') >= 0 || test.indexOf('/customer/toLogin') >= 0) {
                    window.location.reload();
                } else {
                    $("#modal-login-form").modal();
                }
			}else{
				// 优惠券
				$.ajax({
		    		url: ContextPath+"/sidebarComp/openCoupon",
		    		data: {uuid:url, ranNum:Math.random()},
		    		dataType:"html",
					type:"GET",
		    		"success": function (data) {
		    			$("#" + that.compId + "_show").addClass("m_newpeople").html(data);
		    			
		    			$("#m_package").modal();
		    			event.preventDefault();
		    			event.stopPropagation()
		    		}
		    	})
			}
		})
	}
}

 //***********************侧边栏组件结束***********************01


//***********************服务支持-常见问题组件开发***********************
Comps.ServiceFaqComp = function(config){
	this.compId = config.compId;
	this.needAsyncInit = config.needAsyncInit;
	this.pageShow = config.pageShow;
	this.config = config;
	this.nowCategory=0;
	this.content="";
}

Comps.ServiceFaqComp.prototype.init = function(){
	var that = this;
	//1.是否懒加载，如果是懒加载，则下面数据通过ajax请求，渲染html页面
	var compId = this.compId;
	if(this.needAsyncInit){
		that.ajaxLoadData();
		that.ajaxLoadDataCateGory();
	}else{
		Utils.ajaxLoadComplate(that);
	}
	this.bindEvent();
}

//ajax请求数据
Comps.ServiceFaqComp.prototype.ajaxLoadData =function(queryData){
    var that = this;
    var compId = this.compId;
    var subCategoryList = that.config.subCategoryList;
	var subCategoryList2=subCategoryList.split(",");
	var pageShow=this.pageShow;
	var nowCategory= this.nowCategory;
	if(subCategoryList2.length>0 && subCategoryList2.length>nowCategory){
		var categoryUuid=subCategoryList2[nowCategory];
		this.nowCategory=nowCategory+1;
		var defaultData = {
		        "categoryUuid":categoryUuid,
		        "pageShow":pageShow
		    }
		    $.extend(defaultData,queryData);
			$.ajax({
		        "url":ContextPath+"/serviceFaqComp/ajaxLoadData.json",
		        data: defaultData,
		        "success":function(data){
	        		that.genCompHtml(data);
		            Utils.ajaxLoadComplate(that);
		        }
		    })
	}else{
		$("#"+compId+"_content").html("");
		$("#"+compId+"_content").html(that.content);
	}
}

Comps.ServiceFaqComp.prototype.genCompHtml = function (data){
	var nowCategory= this.nowCategory;
	var that = this;
	var compId = this.compId; 
	var moban =$("#"+compId+"_content").find("ol").eq(0);
	var mobanli =moban.find("li").eq(0);
	var title =moban.find("h5").eq(0);
	var introduction =moban.find("div").eq(0);
	var content="";
	if(nowCategory>1){
		moban.css('display','none'); 
	}
	for (var i = 0; i < data.length; ++i) {
		if(i>0){
			introduction.css('display','none'); 
		}
		title.html(data[i].contentTitle);
		introduction.html(data[i].introduction);
		content+=mobanli.prop("outerHTML");
	}
	moban.html(content);
	var mobanhtml=moban.prop("outerHTML");
	that.content +=mobanhtml;
	that.ajaxLoadData();
}

//分类名称
Comps.ServiceFaqComp.prototype.ajaxLoadDataCateGory =function(queryData){
    var that = this;
    var compId = this.compId; 
    var subCategoryList = that.config.subCategoryList;
		var defaultData = {
		        "subCategoryList":subCategoryList
		    }
		    $.extend(defaultData,queryData);
			$.ajax({
		        "url":ContextPath+"/serviceFaqComp/ajaxLoadDataCateGory.json",
		        data: defaultData,
		        "success":function(data){
	        		that.genDataCateGoryHtml(data);
		            Utils.ajaxLoadComplate(that);
		        }
		    })
}

Comps.ServiceFaqComp.prototype.genDataCateGoryHtml = function (data){
	var compId = this.compId; 
	var h4_html2=$("#"+compId+"_catoryName").find("h4").eq(0);
	 var h4_html = h4_html2.clone();
	$("#"+compId+"_catoryName").html("");
	var content="";
	for (var i = 0; i < data.length; ++i) {
		h4_html.html(data[i]);
		if(i>0){
			h4_html.removeClass("active");
		}
		content+=h4_html.prop("outerHTML");
	}
	$("#"+compId+"_catoryName").html(content);
}

//绑定JS
Comps.ServiceFaqComp.prototype.bindEvent = function(){
	var compId = this.compId;
	$(document).on("click",".m-problem .m_title .fr h4", function () {
		var x= $(this).index("h4");
		$(this).addClass("active").siblings("h4").removeClass("active");
		
		$(".m-problembox").eq(x).show().siblings(".m-problembox").hide();
	});
		$(document).on("click",".m-problem h5",function (){
			$(this).toggleClass("active");
			$(this).next().slideToggle(500);
	});
};

/**
 * 我们的优势组件（上）
 */

Comps.OurAdvantageComp = function(config){
	this.compId = config.compId;
	this.config = config;
}

/**
 * 我们的优势组件（下）
 */

Comps.OurAdvantageDownComp = function(config){
	this.compId = config.compId;
	this.config = config;
	this.bindEvent();
}
//绑定JS
Comps.OurAdvantageDownComp.prototype.bindEvent = function(){
  var compId = this.compId;
  $(document).on("mouseover",".help-tab-top>a",function (){
    $(this).attr("data")
    if($(this).is(".help-a-bg") == true){
      $(this).siblings().removeClass('help-a-bg');
      $(".help-tab-bottom").eq($(this).index()).removeClass("dis-n").siblings().addClass("dis-n");
    }
    if($(this).is(".help-a-bg") == false){
      $(this).addClass('help-a-bg').siblings().removeClass('help-a-bg');
    }
    $(".help-tab-bottom").eq($(this).index()).css("display","block").siblings().css("display","none");
  });
};




